package com.guigu.www.cxk.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.www.cxk.Utils.AgeUtils;
import com.guigu.www.cxk.service.CXKLoginService;
import com.guigu.www.mapper.MemberCaseHistoryMapper;
import com.guigu.www.mapper.MemberMapper;
import com.guigu.www.mapper.MemberTrendsMapper;
import com.guigu.www.pojo.Member;
import com.guigu.www.pojo.MemberCaseHistory;
import com.guigu.www.pojo.MemberTrends;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class CXKLoginServiceImpl implements CXKLoginService {
    @Autowired
    MemberMapper memberMapper;

    @Autowired
    MemberTrendsMapper memberTrendsMapper;

    @Autowired
    MemberCaseHistoryMapper memberCaseHistoryMapper;

    /*注册会员*/
    @Override
    public int LogonMember(Member member) {
        member.setRoleId(3);
        member.setMemState(0);
        member.setMemImg("20211027102825.jpg");
        Date date = new Date();
        /*注册时间*/
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = dateFormat.format(date);
        member.setMemCreateDate(format);
        return memberMapper.insert(member);
    }

    @Override
    public Member SelectMePhone(String phone) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_phone", phone);
        return memberMapper.selectOne(wrapper);
    }

    /*登录查询会员*/
    @Override
    public Member LoginMember(Member member) {
        QueryWrapper<Member> wrapper = new QueryWrapper<Member>();
        wrapper.eq("mem_phone",member.getMemPhone());
        wrapper.eq("mem_password",member.getMemPassword());
        return memberMapper.selectOne(wrapper);
    }
    /*根据会员的id查询会员信息*/
    @Override
    public Member MemberUser(int userid) {
        return memberMapper.selectById(userid);
    }

    /*完善会员信息*/
    @Override
    public int UpdateMenmber(Member member) {
        int ageFromBrithTime = AgeUtils.getAgeFromBrithTime(member.getMemBirthday());
        member.setMemAge(ageFromBrithTime);
        QueryWrapper<Member> wrapper = new QueryWrapper<Member>();
        wrapper.eq("mem_id",member.getMemId());
        return memberMapper.update(member,wrapper);
    }

    @Override
    public int UpdatePublish(Member member) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",member.getMemId());
        return memberMapper.update(member,wrapper);
    }

    @Override
    public boolean UpdateImg(String MeImg,int userid) {
         memberMapper.UpdateImg(MeImg,userid);
         return true;
    }

    @Override
    public Member selectAccount(String phone) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_phone",phone);
        return memberMapper.selectOne(wrapper);
    }

    @Override
    public Member updateMePhone(String phone) {
        QueryWrapper<Member> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_phone",phone);
        return memberMapper.selectOne(wrapper);
    }

    @Override
    public int updateMePassword(int mid, String password) {
        return memberMapper.updateMePassword(password,mid);
    }

    @Override
    public int saveMember(MemberTrends memberTrends) {
        return memberTrendsMapper.insert(memberTrends);
    }

    @Override
    public int saveCaseHistory(MemberCaseHistory memberCaseHistory) {
        return memberCaseHistoryMapper.insert(memberCaseHistory);
    }

    @Override
    public MemberTrends SelectTrends(int sid) {
        QueryWrapper<MemberTrends> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",sid);
        return memberTrendsMapper.selectOne(wrapper);
    }

    @Override
    public int updatecase(MemberTrends memberTrends,int sid) {
        QueryWrapper<MemberTrends> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",sid);
        return memberTrendsMapper.update(memberTrends,wrapper);
    }

    @Override
    public MemberCaseHistory selectcasehistoty(int sid) {
        QueryWrapper<MemberCaseHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",sid);
        return memberCaseHistoryMapper.selectOne(wrapper);
    }

    @Override
    public int updatecasehistroy(MemberCaseHistory memberCaseHistory, int sid) {
        QueryWrapper<MemberCaseHistory> wrapper = new QueryWrapper<>();
        wrapper.eq("mem_id",sid);
        return memberCaseHistoryMapper.update(memberCaseHistory,wrapper);
    }
}
